def print_board(board):
    print("   |   |")
    print(" " + board[0] + " | " + board[1] + " | " + board[2])
    print("   |   |")
    print("-----------")
    print("   |   |")
    print(" " + board[3] + " | " + board[4] + " | " + board[5])
    print("   |   |")
    print("-----------")
    print("   |   |")
    print(" " + board[6] + " | " + board[7] + " | " + board[8])
    print("   |   |")

def check_win(board, player):
    if (board[0] == player and board[1] == player and board[2] == player) or \
       (board[3] == player and board[4] == player and board[5] == player) or \
       (board[6] == player and board[7] == player and board[8] == player) or \
       (board[0] == player and board[3] == player and board[6] == player) or \
       (board[1] == player and board[4] == player and board[7] == player) or \
       (board[2] == player and board[5] == player and board[8] == player) or \
       (board[0] == player and board[4] == player and board[8] == player) or \
       (board[2] == player and board[4] == player and board[6] == player):
        return True
    else:
        return False

def check_tie(board):
    for i in range(9):
        if board[i] == " ":
            return False
    return True

def get_move(board, player):
    valid_move = False
    while not valid_move:
        move = input("Player " + player + ", enter your move (1-9): ")
        if move.isdigit() and int(move) >= 1 and int(move) <= 9 and board[int(move)-1] == " ":
            valid_move = True
        else:
            print("Invalid move. Please try again.")
    return int(move)

def play_game():
    board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
    player = "X"
    game_over = False
    while not game_over:
        print_board(board)
        move = get_move(board, player)
        board[move-1] = player
        if check_win(board, player):
            print_board(board)
            print("Player " + player + " wins!")
            game_over = True
        elif check_tie(board):
            print_board(board)
            print("Tie game!")
            game_over = True
        else:
            if player == "X":
                player = "O"
            else:
                player = "X"

play_game()
